package com.gzyj.server.factory.jtt808.down;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gzyj.data.common.VehicleIncrement;
import com.gzyj.jtt808.utils.HexBytesUtil;
import com.gzyj.jtt808.v2013.Package;
import com.gzyj.jtt808.v2019.HeaderEncryptMethod;
import com.gzyj.jtt808.v2019.HeaderMsgBodyProperty;
import com.gzyj.jtt808.v2019.protocol.jtt1078.JTT808_0x9102;
import com.gzyj.server.config.LogConfig;
import com.gzyj.server.config.SessionCache;
import com.gzyj.server.factory.CommonHandler;
import com.gzyj.server.factory.jtt808.NetWorkSerrvice;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class JTT808_0x9102_Handler implements CommonHandler {
    @Override
    public void handler(ChannelHandlerContext ctx, Package msg) throws Exception {

    }

    @Override
    public void handler(ChannelHandlerContext ctx, com.gzyj.jtt808.v2019.Package msg) throws Exception {

    }

    @Override
    public void handler(ChannelHandlerContext ctx, com.gzyj.jtt808.v2011.Package msg) {

    }


    @Autowired
    private NetWorkSerrvice netWorkSerrvice;

    @Override
    public void handler(VehicleIncrement vehicleInfo, String msg) {
        JSONObject command = JSON.parseObject(msg);
        String ipaddress = command.getString("ipAddress");
        String cmdId = command.getString("cmdID");
        Byte channel = command.getByte("channel");
        Byte controlCmd = command.getByte("command");
        Byte turnOffType = command.getByte("turnOffType");
        Byte bitstreamType = command.getByte("bitstreamType");

        Integer seq = command.getIntValue("seq");


        if (StringUtils.isNotEmpty(vehicleInfo.getTerminalprotocoltype()) && vehicleInfo.getTerminalprotocoltype().equalsIgnoreCase("2019")) {
            JTT808_0x9102 jtt808_0x9102 = new JTT808_0x9102();
            HeaderMsgBodyProperty headerMsgBodyProperty = new HeaderMsgBodyProperty();
            headerMsgBodyProperty.setSubcontract(false);
            headerMsgBodyProperty.setEncryptMethod(HeaderEncryptMethod.None);
            headerMsgBodyProperty.setVersionFlag(true);
            jtt808_0x9102.getPackageHeader().setMsgBodyProperty(headerMsgBodyProperty);

            String phoneNumber = ipaddress;
            if (vehicleInfo.getZongduanid().length() <= 12) {
                //2019 单独处理sim卡号
                phoneNumber = HexBytesUtil.ReadBCD(HexBytesUtil.WriteBCD(phoneNumber, 20), 20);
            }

            jtt808_0x9102.getPackageHeader().setTerminalPhoneNo(phoneNumber);
            jtt808_0x9102.getPackageHeader().setProtocolVersion((byte) 1);

            jtt808_0x9102.setChannelNo(channel);
            jtt808_0x9102.setControlCmd(controlCmd);
            jtt808_0x9102.setCloseAVData(turnOffType);
            jtt808_0x9102.setStreamType(bitstreamType);


            ByteBuf data = jtt808_0x9102.encode();
            netWorkSerrvice.send2Veh(phoneNumber, data);
            LogConfig.commondLog.info("808 2019版 9102 下发 音视频实时传输控制 指令 {} {}", phoneNumber, "待实现");


        } else {
            com.gzyj.jtt808.v2013.protocol.jtt1078.JTT808_0x9102 jtt808_0x9102 = new com.gzyj.jtt808.v2013.protocol.jtt1078.JTT808_0x9102();
            com.gzyj.jtt808.v2013.HeaderMsgBodyProperty headerMsgBodyProperty = new com.gzyj.jtt808.v2013.HeaderMsgBodyProperty();

            headerMsgBodyProperty.setSubcontract(false);
            headerMsgBodyProperty.setEncryptMethod(com.gzyj.jtt808.v2013.HeaderEncryptMethod.None);
            jtt808_0x9102.getPackageHeader().setMsgBodyProperty(headerMsgBodyProperty);
            jtt808_0x9102.getPackageHeader().setTerminalPhoneNo(ipaddress);

            jtt808_0x9102.setChannelNo(channel);
            jtt808_0x9102.setControlCmd(controlCmd);
            jtt808_0x9102.setCloseAVData(turnOffType);
            jtt808_0x9102.setStreamType(bitstreamType);


            ByteBuf data = jtt808_0x9102.encode();
            netWorkSerrvice.send2Veh(ipaddress, data);
            if(SessionCache.THStorage.containsKey(ipaddress)){
                Thread th=SessionCache.THStorage.get(ipaddress);
                if(th.isAlive()){
                    th.stop();
                    LogConfig.commondLog.info("808 2013版 9105 下发 停止实时音视频传输状态通知 线程 {}", ipaddress);
                }
            }
            LogConfig.commondLog.info("808 2013版 9102 下发 音视频实时传输控制 指令 {} {}", ipaddress, "待实现");
        }

    }
}
